package com.hf.lookatme.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * Created by JoeShiguang on 2015/5/22.
 */
public class MapUtils {

  private LocationClient locationClient;

  private OnMapLocationReturnedListener mapLocatinReturnedListener;


  public MapUtils(Context context, OnMapLocationReturnedListener mapLocatinReturnedListener) {

    this.mapLocatinReturnedListener = mapLocatinReturnedListener;
    locationClient = new LocationClient(context.getApplicationContext());
  }

  public LocationClient initLocationClient() {

    LocationClientOption option = new LocationClientOption();
    option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
    option.setCoorType("gcj02");//返回的定位结果是百度经纬度，默认值gcj02
    option.setScanSpan(60000);//设置发起定位请求的间隔时间
    option.setIsNeedAddress(true);
    locationClient.setLocOption(option);
    locationClient.registerLocationListener(new LocationListener());

    return locationClient;
  }

  /**
   * 百度地图的位置监听
   */
  public class LocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {

      mapLocatinReturnedListener.onLocationReturned(bdLocation.getAddrStr());
    }
  }

  public interface OnMapLocationReturnedListener {

    void onLocationReturned(String location);
  }
}
